Java program to Reverse a number using do while loop
Java program to Reverse a number using do while loop
Reverse number or digit using do while loop
Example:
num = 456
rev_num = 0
rev_num = rev_num *10 + reminder = 6
num = num/10 = 45
rev_num = rev_num *10 + reminder = 60 + 5 = 65
num = num/10 = 4
rev_num = rev_num *10 + reminder = 650 + 4 = 654
num = num/10 = 0
Reverse number or digit using do while loop
class RevNum
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number:");
int num=sc.nextInt();
int rev=0;
do{
int reminder=num%10;
rev=rev*10+reminder;
num=num/10;
}while(num!=0);
System.out.println("Reversed Numer is: "+rev);
}
}
Comments
Post a Comment
Please do not write any spam link in the comment box.